package com.iring.service.pbx.udpsocket;

import com.iring.service.common.DeviceService;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

/**
 * UDP服务端
 *
 * @author wanggan
 */
public class NettySocket {

    /**
     * 启动端口进行监听数据
     *
     * @param port
     *            监听端口
     * @param deviceService
     *            操作设备信息的服务类
     * @throws InterruptedException
     *             异常
     */
    public void startUdp(Integer port, DeviceService deviceService) throws InterruptedException {
        Bootstrap b = new Bootstrap();
        EventLoopGroup group = new NioEventLoopGroup();
        b.group(group)
                .channel(NioDatagramChannel.class)
                .option(ChannelOption.SO_BROADCAST, true)
                .option(ChannelOption.SO_REUSEADDR, true)
                .handler(new NettyUDPHandler(deviceService));
        b.bind(port).sync();

    }
}
